package LeetCode.LC20;

import java.util.Stack;

/**
 * @author corn
 * @create 2020-07-20-16:08
 */
public class IsValid20 {
    public static void main(String[] args) {
        String s = "()";
        boolean bool = isValid(s);
        System.out.println(bool);
    }
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        int i = 0;
        for (; i < s.length(); i++) {
            if(s.charAt(i)=='('){
                stack.push('(');
                continue;
            }
            if(s.charAt(i)=='['){
                stack.push('[');
                continue;
            }
            if(s.charAt(i)=='{'){
                stack.push('{');
                continue;
            }
            if(s.charAt(i)==')'&& !stack.empty() && stack.peek()=='('){
                stack.pop();
                continue;
            }else if(s.charAt(i)==']'&& !stack.empty() && stack.peek()=='[') {
                stack.pop();
                continue;
            }else if(s.charAt(i)=='}'&& !stack.empty() && stack.peek()=='{'){
                stack.pop();
                continue;
            }else{
                break;
            }
        }
        if(stack.empty() && i == s.length()){
            return true;
        }else{
            return false;
        }
    }
}
